<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午8:03
 */

namespace app\index\controller;

use think\Controller;

/* 管理员管理*/
class WebSetting extends Controller{

    //判断用户是否登录
    /*public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {

            $this->error('对不起,您还没有登录!请先登录!','login/guard');

        }

    }*/

    /*评论列表  ID, 提交人， 内容，    提交时间
        表名： go_content ， go_member
    */
    public function commentList(){
        //页码参数
        $page = input('get.page');
        $lines = input('get.lines');

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        $join = [
              ['go_member m','a.uid=m.uid','LEFT'],
        ];
        $field = 'a.id,m.username,a.content,a.date';

        $res = db('content')
            ->alias('a')
            ->field($field)
            ->join($join)
            ->limit($limit)
            ->order('a.id desc')
            ->select();

        $total = db('content')->field('COUNT(id) as t')->find();

        $msg = [];
        if(!empty($res) && $total['t']!==false){
            $msg['rows'] = $res;
            $msg['total'] = $total['t'];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            $msgList = json_encode($msg,320);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['total'] = 0;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！并没有匹配的数据哟~~';
            $msgList = json_encode($msg,320);
        }else{

            $msg['result'] = 2;
            $msg['tips'] = '网络忙~稍后重试';
            $msgList = json_encode($msg,320);
        }


        exit($msgList);
    }

    /*管理员密码修改
        go_admin
    */
    public function adminPwdChange(){

        $data = [];
        $username = input('post.username');
        $userpass = input('post.userpass');
        $c_userpass = input('post.c_userpass');
        $useremail = input('post.email');

        if($userpass == $c_userpass){
            $data = [];
            $data['username'] = trim($username);
            $data['userpass'] = md5(trim($userpass));
            $data['useremail'] = trim($useremail);
            $data['mid'] = 1; //渠道商一级， 新增的都默认， 写死

            $res = db('admin')->insert($data);
            $box = []; //初始化 消息盒子 用来将结果信息返回给前端
            if($res){
                $box['result'] =1;
                $box['tips'] = '成功修改密码！';

                $box = json_encode($box,320);
                exit($box);
                // $this->success('添加渠道商成功！',url('distributorList'));
            }else{
                // $this->error('添加渠道商失败！');
                $box['result'] =2;
                $box['tips'] = '修改密码失败!';
                $box = json_encode($box,320);
                exit($box);
            }
        }
        else{

            $box['result'] =2;
            $box['tips'] = '两次输入密码不一样！!';
            $box = json_encode($box,320);
            exit($box);
            // $this->error('两次输入密码不一样！');
        }

    }

    /* 修改管理员密码页面 的 回现管理员的用户名 和 UID */
    public function show_admin_name(){
        $admin_name = session('user');
        $res = db('admin')->field('uid,username')->where(['username'=>$admin_name])->find();
        if(!empty($res)){
            $box = [
                'rows' => $res,
                'result' => 1,
                'tips' => '查询成功！'
            ];
            exit(json_encode($box,320));
        }else{
            $box = [
                'result' =>2,
                'tips' => '用户未找到~'
            ];
            exit(json_encode($box,320));
        }
    }

    /* 域名绑定; 绑定的域名, 绑定的模块,  绑定的控制器, 绑定的函数,  管理操作 */
    public function dnsBindList(){

        //页码参数
        $page = input('get.page');
        $lines = input('get.lines');

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        $res = db('domain_bind')
            ->limit($limit)
            ->order('id desc')
            ->select();
        $total = db('domain_bind')->field('COUNT(id) as t')->find();

        $msg = [];
        if(!empty($res) && $total['t']!==false){
            $msg['rows'] = $res;
            $msg['total'] = $total['t'];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            $msgList = json_encode($msg,320);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['total'] = 0;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！并没有匹配的数据哟~~';
            $msgList = json_encode($msg,320);
        }else{

            $msg['result'] = 2;
            $msg['tips'] = '网络忙~稍后重试';
            $msgList = json_encode($msg,320);
        }


        exit($msgList);

    }


    /*domain_bind_add
        go_domain_bind
    */
    public function domain_bind_add(){
        $data = [];

        $data['domain'] = input('post.domain');
        $data['module'] = input('post.module');
        $data['controller'] = input('post.controller');
        $data['func'] = input('post.func');

        $res = db('domain_bind')->insert($data);
        if($res){
            $msg['result'] = 1;
            $msg['tips'] = '添加成功！';
            $msgList = json_encode($msg,320);
        }

        exit($msgList);

    }


    /*域名绑定 修改
        表： go_domain_bind
    */
    public function domain_bind_edit(){
        $data = [];

        $id = input('post.id');
        $data['domain'] = input('post.domain');
        $data['module'] = input('post.module');
        $data['controller'] = input('post.controller');
        $data['func'] = input('post.func');

        $res = db('domain_bind')->where(['id'=>$id])->update($data);
        if($res!==false){
            $msg['result'] = 1;
            $msg['tips'] = '修改成功！';
            $msgList = json_encode($msg,320);
            exit($msgList);
        }
        else{
            $msg['result'] = 2;
            $msg['tips'] = '修改失败！';
            $msgList = json_encode($msg,320);
            exit($msgList);
        }
    }




    /*手机幻灯片管理  列表
        表名： go_wap
    */
    public function mobileBanners(){
//        $banners_path = ROOT_PATH . 'public/';


        //页码参数
        $page = input('get.page');
        $lines = input('get.lines');

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;

        $res = db('wap')
            ->limit($limit)
            ->order('id desc')
            ->select();

        $total = db('wap')->field('COUNT(id) as t')->find();

        foreach($res as $k=>&$v){
            if($v['img']){
                $v['img'] = '/uploads/'. $v['img'];
            }
        }

        $msg = [];
        if(!empty($res) && $total['t']!==false){
            $msg['rows'] = $res;
            $msg['total'] = $total['t'];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            $msgList = json_encode($msg,320);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['total'] = 0;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！并没有匹配的数据哟~~';
            $msgList = json_encode($msg,320);
        }else{

            $msg['result'] = 2;
            $msg['tips'] = '网络忙~稍后重试';
            $msgList = json_encode($msg,320);

        }

        exit($msgList);

    }

    /* 接收保存用户提交的意见 */
    public function suggestion_rec(){
        $uid = input('post.uid');
        $content = input('post.content');
        $content = htmlspecialchars($content);  //过滤html 代码。 防御 xss 攻击
        if($uid && $content){
            $res = db('content')->insert([

                'uid' => $uid,
                'content' => $content,
                'date' => date('Y-m-d H:i:s',time()),
            ]);

            $msg = [];
            if($res){
                
                $msg = [
                    'result' =>1,
                    'tips' => '建议提交成功！'
                ];

                exit(json_encode($msg,320));
            }//if
            else{
                $msg = [
                    'result' =>2,
                    'tips' => '网络缓慢， 请稍后再提交！'
                ];

                exit(json_encode($msg,320));
            }
        }//if
    }//func 

    /* 返回banner 路径给 app 前端 */
    public function get_banners(){
        
        $res = db('wap')->field('id,img')->select();
        if(!empty($res)){
            foreach($res as &$v){
                $v['img'] = 'http://wyq.weikj.net:86/uploads/' . $v['img'];
            }
        }
        
        if($res && $res !== false){
            $msg = [
                'result' =>1,
                'tips' => '查询成功！',
                'rows' => $res
            ];

            exit(json_encode($msg,320));
        }
        elseif(empty($res)){
            $msg = [
                'result' =>1,
                'tips' => '查询成功！并无任何数据',
                'rows' => $res
            ];

            exit(json_encode($msg,320));
        }
        else{
            $msg = [
                'result' =>2,
                'tips' => '网络延迟~'
            ];

            exit(json_encode($msg,320));
        }
    }//func get_banners

    /*修改 banner
        表： go_wap
     */
    public function banner_info_edit(){

        $data = [];
        if(isset($_FILES['img']['tmp_name'])){//如果有传图片
            $data['img'] = $this->upload();
        }

        $id = input('post.id/d');
        $data['title'] = input('post.title/s');
        $data['link'] = input('post.link');
        $data['color'] = input('post.color');

        $res = db('wap')->where(['id'=>$id])->update($data);

        $msg = [];
        if($res !== false){
            $msg = [
                'result' =>1,
                'tips' => '更新轮播图成功！',
            ];

            exit(json_encode($msg,320));
        }else{
            $msg = [
                'result' =>2,
                'tips' => '更新轮播图失败！',
            ];

            exit(json_encode($msg,320));
        }


    }//func banner edit


    /*banner add    轮播图添加*/
    public function banner_add(){
        $data = [];
        if($_FILES['img']['tmp_name']){//如果有传图片
            $data['img'] = $this->upload();
        }
        $data['title'] = input('post.title');
        $data['link'] = input('post.link');
        $data['color'] = input('post.color');

        $res = db('wap')->insert($data);
        if($res){
            $msg = [
                'result' =>1,
                'tips' => '添加轮播图成功！',
            ];
            exit(json_encode($msg,320));
        }
        else{
            $msg = [
                'result' =>2,
                'tips' => '添加轮播图失败！',
            ];

            exit(json_encode($msg,320));
        }
    }





    /*轮播图删除*/
    public function banner_del(){
        $id = input('post.id/d');
        db('wap')->delete($id);

        $msg = [
            'result' =>1,
            'tips' => '更轮播图信息删除成功！！',
        ];

        exit(json_encode($msg,320));
    }


    /*清空缓存  并不存在什么缓存  如果有， 就是清空 redis 缓存  毫无意义*/
    public function cacheClear(){
        session('main_data',null);
    }


    /*图片上传类*/
    public function upload(){


        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('img');


        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            //  /Library/WebServer/Documents/douhua/public/uploads/banner
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'banner');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                /*echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();*/

                return 'banner/' . $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }//else
        }//if
    }//func upload




    /*   公告   */
    public function gonggao(){


        $join = [
            ['go_shoplist b','a.shopid=b.id','LEFT'],
        ];

        $res = db('news')
            ->alias('a')
            ->field('a.mobile,b.title,a.money')
            ->join($join)
            ->limit('0,10')->select();

        foreach($res as &$v){
            $v['mobile'] = $this->encrypt($v['mobile']);
        }

        if($res){
            $box['rows'] = $res;
            $box['result'] =1;
            $box['tips'] = '查询成功！';
            exit(json_encode($box,320));
        }
        else{
            $box['result'] =2;
            $box['tips'] = '网络延迟， 请稍后再试';
            exit(json_encode($box,320));
        }
    }//func gonggao

    public function encrypt($m){ //输入手机 ， 输出加密后的状态
        //字符串截取 substr
        $p = substr($m,0,3)."****".substr($m,7,4);

        return $p;
    }



}